<?php

require_once 'Ext/Register/Save/Abstract.php';

class Ext_Register_Save_Adapter_Ucenter extends Ext_Register_Save_Abstract
{
    const USERNAME_FORMAT_ERROR              = 'usernameFormatError';
    const USERNAME_CONTAIN_DENIED_WORD       = 'usernameContainDeniedWord';
    const USERNAME_ALREADY_USED              = 'usernameAlreadyUsed';
    const EMAIL_FORMAT_ERROR                 = 'emailFormatError';
    const EMAIL_IS_DENIED                    = 'emailIsDenied';
    const EMAIL_ALREADY_USED                 = 'emailAlreadyUsed';
    const UNCATEGORIZED                      = 'uncategorized';
    protected $_messageTemplates = array(
        self::USERNAME_FORMAT_ERROR               => "The username '%name%' format error.",
        self::USERNAME_CONTAIN_DENIED_WORD        => "The username '%name%' contain deined word.",
        self::USERNAME_ALREADY_USED               => "The username '%name%' is already used.",
        self::EMAIL_FORMAT_ERROR                  => "The email '%name%' format error.",
        self::EMAIL_IS_DENIED                     => "The email '%name%' is denied.",
        self::EMAIL_ALREADY_USED                  => "The email '%name%' is already used.",
        self::UNCATEGORIZED                       => "uncategorized.",
    );

    public function save()
    {
        $result = array(
            'code'        => Ext_Register_Save_Result::FAILURE,
            'data'        => '',
            'messages'    => array(),
        );
        
        $validater = new Ext_Validate();
        $validater->addValidater(new Ext_Validate_Adapter_Captcha($this->_data->captcha));
        $validater->addValidater(new Ext_Register_Validate_Email($this->_data->email));
        $validater->addValidater(new Ext_Register_Validate_Username($this->_data->username));
        $validater->addValidater(new Ext_Register_Validate_Password($this->_data->password, $this->_data->confirmPassword));
        if(!$validater->isValid()){
            $result['messages'] = $validater->getMessages();
            return new Ext_Register_Save_Result($result['code'], $result['data'], $result['messages']);
        }

        $ucenter = new Ext_Ucenter();
        $uid = $ucenter->register($this->_data->username, $this->_data->password, $this->_data->email);

        if($uid < 1){
            if($uid == -1) {
                $result['messages'][]     = $this->_messager->createMessage(self::USERNAME_FORMART_ERROR, array('name'=>$this->_data->username));

            } elseif($uid == -2) {
                $result['messages'][]     = $this->_messager->createMessage(self::USERNAME_CONTAIN_DENIED_WORD, array('name'=>$this->_data->username));
                
            } elseif($uid == -3) {
                $result['messages'][]     = $this->_messager->createMessage(self::USERNAME_ALREADY_USED, array('name'=>$this->_data->username));

            } elseif($uid == -4) {
                $result['messages'][]     = $this->_messager->createMessage(self::EMAIL_FORMAT_ERROR, array('name'=>$this->_data->email));

            } elseif($uid == -5) {
                $result['messages'][]     = $this->_messager->createMessage(self::EMAIL_IS_DENIED, array('name'=>$this->_data->email));

            } elseif($uid == -6) {
                $result['messages'][]     = $this->_messager->createMessage(self::EMAIL_ALREADY_USED, array('name'=>$this->_data->email));

            } else {
                $result['messages'][]     = $this->_messager->createMessage(self::UNCATEGORIZED);
                
            }
            
            return new Ext_Register_Save_Result($result['code'], $result['data'], $result['messages']);
        }
    
        $settings = Zend_Registry::get('settings');
        $row = array(
            'nickname'  => $this->_data->nickname,
            'roleId'    => $settings['userDefaultRoleId'],
        );
        $userId = App_Model_DbTable_User::getInstance()->addNew($row);
    
        $row = array(
            'username'  => $this->_data->username,
            'userId'    => $userId,
        );
        App_Model_DbTable_User_Account_Base_Username::getInstance()->addNew($row);

        $row = array (
            'userId'   => $userId,
            'username' => $this->_data->username,
        );
        App_Model_DbTable_User_Profile::getInstance()->addNew($row);
        
        $result['code'] = Ext_Register_Save_Result::SUCCESS;
        $result['data'] = $userId;
        return new Ext_Register_Save_Result($result['code'], $result['data'], $result['messages']);
    }
}
